class Uv < Formula
  desc "Extremely fast Python package installer and resolver, written in Rust"
  homepage "https://docs.astral.sh/uv/"
  url "https://github.com/astral-sh/uv/archive/refs/tags/0.8.9.tar.gz"
  sha256 "a3164652c0da2ad7781184f761ccb2153a17b1ef6f00e7a75d59fbd5a2a4dac2"
  license any_of: ["Apache-2.0", "MIT"]
  head "https://github.com/astral-sh/uv.git", branch: "main"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_sequoia: "ac47828f0d373a695d61e6ba2b3470b99494024f138ce24d8d87120c2f68f9ab"
    sha256 cellar: :any_skip_relocation, arm64_sonoma:  "4f6a22cf8eb38ba998d1dec7c619a9a803137ffb862e9e052c69dde976748bcf"
    sha256 cellar: :any_skip_relocation, arm64_ventura: "07edf3e1eac2395e976627c2aa6c01fc71e7b9fbc879fffb3bec580190e985c8"
    sha256 cellar: :any_skip_relocation, sonoma:        "c5e68526f269a4b24007fbd6d6cd70ad92545c203981faf9b293c780b28accbd"
    sha256 cellar: :any_skip_relocation, ventura:       "17b82ab0149af6fbd195370b386a9945b3c87ba070ef9baba9f48c8893b21342"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "829aa4d80b5f7943ab7cbab2615d6c8f77cc56bc6dbc46a44d42c794615668d2"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "12d5e03e27ed3c15239170c63d838c128f744a5f09977183d6dbbb213d0f3990"
  end

  depends_on "pkgconf" => :build
  depends_on "rust" => :build

  uses_from_macos "python" => :test
  uses_from_macos "bzip2"
  uses_from_macos "xz"

  def install
    ENV["UV_COMMIT_HASH"] = ENV["UV_COMMIT_SHORT_HASH"] = tap.user
    ENV["UV_COMMIT_DATE"] = time.strftime("%F")
    system "cargo", "install", *std_cargo_args(path: "crates/uv")
    generate_completions_from_executable(bin/"uv", "generate-shell-completion")
    generate_completions_from_executable(bin/"uvx", "--generate-shell-completion")
  end

  test do
    (testpath/"requirements.in").write <<~REQUIREMENTS
      requests
    REQUIREMENTS

    compiled = shell_output("#{bin}/uv pip compile -q requirements.in")
    assert_match "This file was autogenerated by uv", compiled
    assert_match "# via requests", compiled

    assert_match "ruff 0.5.1", shell_output("#{bin}/uvx -q ruff@0.5.1 --version")
  end
end
